package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.entity.HouseHistory;
import com.yjps.house.model.House;
import com.yjps.house.service.HouseService;
import com.yjps.house.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house/publish")
public class PublishController {

    @Autowired
    HouseService houseService;
    @Autowired
    PublishService publishService;

    /**
     * 经纪人发布房源
     *
     * @param json { "publish": { "agentId": "1" }, "house": { "buildingId": "", "title": "", "description": "", "imageUrl": "", "area": "", "price": "", "sellType": "", "detail": "" }
     * @return json 成功返回{data:{"history_id":""}}
     */
    @RequestMapping(value = "/agents/edit", method = RequestMethod.POST)
    @ResponseBody
    public String publish(@RequestParam(value = "publish") String json) {
        //解析json
        JSONObject object = JSON.parseObject(json);
        JSONObject publish = JSON.parseObject(object.getString("publish"));
        String agentsId = publish.getString("agentsId");
        String time = TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);
        House house = JSON.parseObject(object.getString("house"), House.class);

        //调用业务
        int houseId = publishService.publishHouse(house, agentsId, time);

        //封装返回
        Message message;
        Map<String,Object> map=new HashMap<>();
        if (houseId>0){
            map.put("houseId",houseId);
            message=new Message(true,"发布房源成功",map);
        }else {
            message=new Message(false,"发布房源失败");
        }
        return message.toJson();
    }

    /**
     * 经纪人获取房源发布记录
     * @param json {"agentsId":"","page":"","pageSize":"","queryType":"all/verfiy/unverfiy"}
     * @return 成功返回{data:{publishHistory:{...}...}}
     */
    @RequestMapping(value = "/agents/list",method =RequestMethod.POST )
    @ResponseBody
    public String agentsList(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object = JSON.parseObject(json);
        int agentsId=Integer.parseInt(object.getString("agentsId"));
        int pageSize=Integer.parseInt(object.getString("pageSize"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*pageSize;
        String queryType=object.getString("queryType");

        //调用业务
        boolean flag=true;
        List<HouseHistory> histories =new ArrayList<>();
        if("all".equals(queryType)){
            histories =publishService.allListByAgentsId(agentsId, startRow, pageSize);
        }else if ("verfiy".equals(queryType)){
            histories =publishService.verfiyListByAgentsId(agentsId, startRow, pageSize);
        }else if ("unverfiy".equals(queryType)){
            histories =publishService.unVerfiyListByAgentsId(agentsId, startRow, pageSize);
        }else{
            //参数错误
            flag=false;
        }

        //封装返回
        Message message;
        if (!histories.isEmpty()){
            message=new Message(true,"查到些房源记录", histories);
        }else if(flag){
            message=new Message(true,"什么都没查到");
        }else{
            message=new Message(false,"参数错误");
        }
        return message.toJson();
    }

    /**
     * 门店经理获取房源发布记录
     * @param json {"shopId":"","page":"","pageSize":"","queryType":"all/verfiy/unverfiy"}
     * @return 成功返回{data:{publishHistory:{...}...}}
     */
    @RequestMapping(value = "/shop/list",method =RequestMethod.POST )
    @ResponseBody
    public String shopList(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object = JSON.parseObject(json);
        int shopId=Integer.parseInt(object.getString("shopId"));
        int pageSize=Integer.parseInt(object.getString("pageSize"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*pageSize;
        String queryType=object.getString("queryType");

        //调用业务
        boolean flag=true;
        List<HouseHistory> histories =new ArrayList<>();
        if("all".equals(queryType)){
            histories =publishService.allListByShopId(shopId, startRow, pageSize);
        }else if ("verfiy".equals(queryType)){
            histories =publishService.verfiyListByShopId(shopId, startRow, pageSize);
        }else if ("unverfiy".equals(queryType)){
            histories =publishService.unVerfiyListByShopId(shopId, startRow, pageSize);
        }else{
            //参数错误
            flag=false;
        }

        //封装返回
        Message message;
        if (!histories.isEmpty()){
            message=new Message(true,"查到些房源记录", histories);
        }else if(flag){
            message=new Message(true,"什么都没查到");
        }else{
            message=new Message(false,"参数错误");
        }
        return message.toJson();
    }


    /**
     * 撤销房源
     * @param {"houseId":""}
     * @return
     */
    @RequestMapping(value = "/cancel",method =RequestMethod.POST )
    @ResponseBody
    public String cancelPublish(@RequestParam(value = "houseId") String houseId){
        int houseID=Integer.parseInt(houseId);
        Message message;
        if (houseID<=0){
            message=new Message(false,"参数错误");
        }else{
            publishService.cancelPublish(houseID);
            message=new Message(true,"撤销房源成功");
        }
        return message.toJson();
    }
}
